package com.xinli.xinlifm.view.fragment.home;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.xinli.xinlifm.App;
import com.xinli.xinlifm.R;
import com.xinli.xinlifm.http.bean.HomeBean;
import com.xinli.xinlifm.mvp.iview.MvpView;
import com.xinli.xinlifm.mvp.model.SuperModel;
import com.xinli.xinlifm.view.adapter.HomeRecyclerAdapter;
import com.xinli.xinlifm.view.fragment.BaseFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * 首页
 * Created by zz on 16-10-12.
 */

public class HomeFragment extends BaseFragment<SuperModel> implements MvpView<HomeBean> {

    public SQLiteDatabase mDatabase;
    public String mPage;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        mDatabase = App.xinLiSqlHelper.getReadableDatabase();
        mPage = HomeBean.class.getName() + "1";

        Cursor cursor = mDatabase.query("cache", null, "page=?", new String[]{mPage}, null, null, null);
        if (cursor.moveToFirst()) {
            String content = cursor.getString(cursor.getColumnIndex("content"));
            HomeBean homeBean = new Gson().fromJson(content, HomeBean.class);
            SetHomeAdapter(homeBean);
            mDatabase.close();
        }
        else {
            getModel().getModelData();

        }


    }

    @Subscribe
    @Override
    public void showView(HomeBean data) {
        Log.e("自定义标签", "类名==HomePresenter" + "方法名==getHomeData=====:" + data);
        mDatabase = App.xinLiSqlHelper.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("page", HomeBean.class.getName() + "1");
        values.put("content", new Gson().toJson(data));
        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format.format(date);
        values.put("time", time);
        mDatabase.insert("cache", null, values);
        mDatabase.close();
        SetHomeAdapter(data);
        //请求回来的时候取消注册不然会出现，已经注册的错误
        EventBus.getDefault().unregister(this);
    }

    private void SetHomeAdapter(HomeBean data) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        base_recyvlerview.setLayoutManager(layoutManager);
        ArrayList<Object> list = new ArrayList<>();
        list.add(data.getData().getTuijian());
        list.add(data.getData().getCategory());
        list.add(data.getData().getHotfm());
        list.add(data.getData().getNewlesson());
        list.add(data.getData().getNewfm());
        list.add(data.getData().getDiantai());
        HomeRecyclerAdapter adapter = new HomeRecyclerAdapter(getContext(), list, new int[]{R.layout.home_viewpager_item, R.layout.home_category_iten, R.layout.home_hotfm_item, R.layout.home_newfm_item, R.layout.home_diantai_item, R.layout.download});
        base_recyvlerview.setAdapter(adapter);
    }

    @Override
    public SuperModel getModel() {
        return new SuperModel();
    }

}
